// socketing.cpp : Defines the entry point for the console application.
//
#include <winsock2.h>
#include <stdio.h>

#include "../Logger/Logger.h"
#include "../CommunicationControl/CommDefs.h"
int main(int argc, char* argv[])
{
	int					Ret = 0 ;
	WSADATA             wsaData;
	SOCKADDR_IN			ClientAddr;
	int                 Port = 5150;
	SOCKET				ClientSocket;
	
	ClientAddr.sin_family = AF_INET;
	ClientAddr.sin_port = htons(Port);    
	ClientAddr.sin_addr.S_un.S_un_b.s_b1 = 127;
	ClientAddr.sin_addr.S_un.S_un_b.s_b2 = 0;
	ClientAddr.sin_addr.S_un.S_un_b.s_b3 = 0;
	ClientAddr.sin_addr.S_un.S_un_b.s_b4 = 1;
	
	if ((Ret = WSAStartup(MAKEWORD(2,2), &wsaData)) != 0)
	{
		// NOTE: Since Winsock failed to load we cannot use 
		// WSAGetLastError to determine the specific error for
		// why it failed. Instead we can rely on the return 
		// status of WSAStartup.
		sLogMsgf<LogLevel<LOG_LEVEL_HIGH>,LineType<LOG_TYPE_EXCEPTION> >("WSAStartup failed with error %d\n", Ret);
		return Ret;
	}
	ClientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (connect(ClientSocket,(SOCKADDR*)&ClientAddr,sizeof(ClientAddr)) != 0)
	{
		sLogMsgf<LogLevel<LOG_LEVEL_HIGH>,LineType<LOG_TYPE_EXCEPTION> >("connect failed with error %d\n", WSAGetLastError());
		return Ret;
	}

	int times = 1000;
	while (times-->0)
	{
		char	sBuff[256] = "OLA KEPASA?";
		Ret = send(ClientSocket,sBuff,strlen(sBuff),NULL);
		if ( Ret == -1)
		{
			sLogMsgf<LogLevel<LOG_LEVEL_HIGH>,LineType<LOG_TYPE_EXCEPTION> >("send failed with error %d\n", WSAGetLastError());
			return Ret;
		}
		sLogMsgf<LogLevel<LOG_LEVEL_NORMAL>,LineType<LOG_TYPE_NOTIFICATION> >("sent data %ld\n", Ret);
		Sleep(100);
	}
	return 0;
}

